#ifndef __CC_MSG__
#define __CC_MSG__
#include "cc_core.h"

extern int g_msg_current_send_node;

typedef void (*cc_msg_enter_cb)(void* user_data, const char* data, char* topic,
                                int node);
// 可以用来free内存等
typedef void (*cc_msg_exit_cb)(void* user_data, const char* data, char* topic,
                               int node);

typedef struct
{
  cc_que_t que;
  cc_mutex_t send_mut;
  cc_mempool_t pool;

  // char topic[CC_CONFIG_MSG_TOPIC_MAX_LEN];
} cc_msg_node;

typedef struct
{
  char* topic;
  char* data;
  uint8_t data_len;
  uint8_t topic_len;
  cc_msg_enter_cb enter_cb;
  cc_msg_exit_cb exit_cb;
  int node;
} cc_msg_frame;

typedef enum
{
  CC_MSG_SERIAL_NODE = 0,
  CC_MSG_MQTT_NODE,
  CC_MSG_BLUETOOTH_NODE,
  CC_MSG_SOCKET_NODE,

  CC_MSG_NODE_MAX
} e_cc_msg_node;

typedef int (*cc_msg_handle_fun)(cc_mnode_t node);
typedef struct
{
  char prefix[CC_CONFIG_MSG_HANDLE_PREFIX_MAX_LEN];
  cc_msg_handle_fun fun;
} msg_handle_main;

void cc_msg_handle_register(const char* prefix, cc_msg_handle_fun fun);

#define CC_MSG_HANDLE_REGISTER(func, prefix_name)                  \
  void __attribute__((constructor)) cc_msg_handle_init##func(void) \
  {                                                                \
    cc_msg_handle_register(prefix_name, func);                     \
  }

// 发送给节点的
int cc_msg_send(int node, char* data, int len, char* topic, int topic_len);
void cc_msg_resp(int node, const char* data, int len, const char* topic);
#endif